#!/usr/bin/envpython
# -*-coding:UTF-8-*-
'''
@File    :   base.py
@Contact :   308711822@qq.com
@License :   (C) Copyright 2021-2225, Personal exclusive right.

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/1/31 16:25   小钟同学      1.0         None
'''
import abc
from typing import overload, Callable, Dict, Union, List, Any

from fastapi import Request, Depends, Response
from sqlalchemy.ext.asyncio import AsyncSession

from infirmary_admin_src.infirmary_common.infirmary_responses.orjson_response import SnowySuccess, SnowyFail




class IBaseController(metaclass=abc.ABCMeta):
    def __init__(
            self,
            request: Request
    ):
        self._request: Request = request
        # 业务逻辑处理是否成功
        self.response_ok: bool = True
        # 提示信息
        self.response_msg: Union[str, None] = None
        # 设置响应编码
        self.response_api_code: Union[int, None] = None
        # 是否自定义返回响应报文，针对微信的回调处理
        self.response_curr: bool = False


    #  # @async_property
    #  # 如果被async_property装饰则下面的判断asyncio.iscoroutinefunction(infirmary_controller.response)会失效
    @property
    def request(self) -> Request:
        # 直接处理生成结果
        return self._request

    @abc.abstractmethod
    @overload
    async def business_login(self) -> Any:
        raise NotImplemented

    @abc.abstractmethod
    def business_login(self) -> Any:
        raise NotImplemented

    async def async_response(self):
        # 直接处理生成结果
        if not self.response_curr:
            result = await self.business_login()
            if self.response_api_code:
                return SnowySuccess(api_code=self.response_api_code,data=result,
                                    message=self.response_msg) if self.response_ok else SnowyFail(
                    api_code=self.response_api_code,message=self.response_msg)
            return SnowySuccess(data=result,
                                message=self.response_msg) if self.response_ok else SnowyFail(
                message=self.response_msg)
        else:
            return await self.business_login()

    def sync_response(self):
        if not self.response_curr:
            result = self.business_login()
            if self.response_api_code:
                return SnowySuccess(api_code=self.response_api_code,data=result,
                                    message=self.response_msg) if self.response_ok else SnowyFail(
                    api_code=self.response_api_code,message=self.response_msg)
            return SnowySuccess(data=result,
                                message=self.response_msg) if self.response_ok else SnowyFail(
                message=self.response_msg)
        else:
            return self.business_login()



class IAsyncSessionBaseController(IBaseController):
    def __init__(
            self,
            request: Request,
            async_session: Depends,
    ):
        super().__init__(request)
        self._async_session: AsyncSession = async_session

    @property
    def async_session(self) -> AsyncSession:
        # 直接处理生成结果
        return self._async_session
